package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfTeamGroupBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamGroupEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamGroupAdapter;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamGroupModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * description:个人中心-班组长
 * @author liwei
 * @date 2020/10/19
 */
public class SelfTeamGroupActivity extends AppActivity {

    private AtSelfTeamGroupBinding binding;

    @Inject
    SelfTeamGroupModel viewModel;

    private SelfTeamGroupAdapter mAdapter;

    public static final int REQUEST_EDIT = 10005;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamGroupActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);

    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                SelfTeamGroupEditActivity.start(SelfTeamGroupActivity.this, REQUEST_EDIT);
            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_team_group);
        binding.setViewModel(viewModel);
        if (mAdapter == null) {
            mAdapter = new SelfTeamGroupAdapter(this, null, Glide.with(this));
        }
        mAdapter.setListener(new SelfTeamGroupAdapter.GroupRecyclerItemClick() {

            @Override
            public void delete(SelfTeamGroupEntity entity) {
                optState(entity);
            }

            @Override
            public void edit(SelfTeamGroupEntity entity) {
                SelfTeamGroupEditActivity.start(SelfTeamGroupActivity.this, entity.getNo(), REQUEST_EDIT);
            }

            @Override
            public void onItemClick(SelfTeamGroupEntity entity) {
                viewModel.getH5Url(entity.getNo());
            }

        });
        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.requestTeamGroupList(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.requestTeamGroupList(true);
            }
        });
        binding.recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void init() {
        viewModel.requestTeamGroupList(false);
    }

    public void showList(List<SelfTeamGroupEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void optState(SelfTeamGroupEntity entity) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.deleteTeamGroup(entity);
                    }

                    @Override
                    public void onLeftClick() {
                    }
                }).show();
    }

    public void deleteUpdateView(SelfTeamGroupEntity entity) {
        mAdapter.removeItem(entity);
        if (mAdapter.getItemCount() == 0) {
            binding.emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_EDIT) {
                viewModel.requestTeamGroupList(false);
            }
        }
    }

    public void showEmpty(){
        binding.recyclerView.loadMoreComplete();
        binding.recyclerView.refreshComplete();
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.recyclerView.setLoadingMoreEnabled(false);
    }
}
